package com.hyperfix.washer.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hyperfix.common.auth.RequiresRole;
import com.hyperfix.models.common.R;
import com.hyperfix.models.user.dtos.UserInfoDTO;
import com.hyperfix.models.washer.dtos.PipelineDTO;
import com.hyperfix.models.washer.dtos.PipelineS;
import com.hyperfix.models.washer.dtos.PipelineT;
import com.hyperfix.models.washer.pojos.Pipeline;
import com.hyperfix.models.washer.vos.PipelineQueryVO;
import com.hyperfix.models.washer.vos.PipelineVO;
import com.hyperfix.washer.service.PipelineService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author JiangChunYu
 * 2024/4/14
 */
@Validated
@RestController
@RequestMapping("/pipeline")
public class PipelineController {
    @Resource
    private PipelineService pipelineService;

    /**
     * 查询管线列表
     *
     * @param pageNum         页码
     * @param size            大小
     * @param pipelineQueryVO 管线信息查询 VO
     * @return {@link R}<{@link IPage}<{@link UserInfoDTO}>> 查询结果
     */
    @PostMapping("/list/{pageNum}/{size}")
    public R<IPage<PipelineDTO>> list(@PathVariable @Min(value = 1, message = "页号最小不能小于1") Integer pageNum,
                                      @PathVariable @Min(value = 1, message = "页大小最小不能小于1") Integer size,
                                      @RequestBody @Validated PipelineQueryVO pipelineQueryVO) {
        return pipelineService.list(pageNum, size, pipelineQueryVO);
    }

    /**
     * 查询所有管线 附带状态
     *
     * @return {@link R}<{@link List}<{@link PipelineT}>> 查询结果
     */
    @PostMapping("/listAll")
    public R<List<PipelineT>> listAll() {
        List<PipelineT> pipelineTS = pipelineService.listAllWithStatus();
        return R.success(pipelineTS);
    }

    /**
     * 分组查询所有管线 以站场分组
     *
     * @return {@link R}<{@link List}<{@link PipelineS}>>
     */
    @PostMapping("/listWithStation")
    public R<List<PipelineS>> listAllWithStation() {
        return pipelineService.listAllWithStation();
    }

    /**
     * 查询当前用户初始监控管线，没有就新增一条数据
     *
     * @return {@link R}<{@link Pipeline}>
     */
    @PostMapping("/query")
    public R<Pipeline> queryInit() {
        return pipelineService.queryInit();
    }

    /**
     * 切换监控管线
     *
     * @param pipelineName 管线名称
     * @return {@link R}<{@link String}> 是否切换成功
     */
    @PostMapping("/toggle/{pipelineName}")
    public R<String> togglePipeline(@PathVariable("pipelineName") @NotEmpty String pipelineName) {
        return pipelineService.togglePipeline(pipelineName);
    }

    /**
     * 禁用/启用管线
     *
     * @param pipelineId 管线 ID
     * @param stop       1: 启用 0: 禁用
     * @return {@link R}<{@link String}> 是否禁用/启用成功
     */
    @RequiresRole("admin")
    @PostMapping("/enable/{pipelineId}/{stop}")
    public R<String> enablePipeline(@PathVariable("pipelineId") @NotNull Integer pipelineId,
                                    @PathVariable("stop") @NotNull @Max(1) @Min(0) Integer stop) {
        return pipelineService.enablePipeline(pipelineId, stop);
    }

    /**
     * 设置采集周期
     *
     * @param pipelineId 管线 ID
     * @param period     周期 单位: s
     * @return {@link R}<{@link String}> 是否设置成功
     */
    @RequiresRole("admin")
    @PostMapping("/cycle/{pipelineId}/{period}")
    public R<String> changeCycle(@PathVariable("pipelineId") @NotNull Integer pipelineId,
                                 @PathVariable("period") Long period) {
        return pipelineService.changeCycle(pipelineId, period);
    }

    /**
     * 修改管线信息
     *
     * @param pipelineVO 管线信息 VO
     * @return {@link R}<{@link String}> 是否修改成功
     */
    @RequiresRole("admin")
    @PostMapping("/update")
    public R<String> updatePipeline(@RequestBody @Validated PipelineVO pipelineVO) {
        return pipelineService.updatePipeline(pipelineVO);
    }
}
